package org.dru.clay.respository.artifact;

public class UnresolvedArtifact implements Comparable<UnresolvedArtifact> {
	private final String name;
	private final VersionPattern version;
	private final String extension;
	private final String classifier;

	public UnresolvedArtifact(String name, VersionPattern version, String extension, String classifier) {
		this.name = name;
		this.version = version;
		this.extension = extension;
		this.classifier = classifier;
	}

	public UnresolvedArtifact(String name, VersionPattern version) {
		this.name = name;
		this.version = version;
		this.extension = null;
		this.classifier = null;
	}

	public String getName() {
		return name;
	}

	public VersionPattern getVersionPattern() {
		return version;
	}

	public String getExtension() {
		return extension;
	}

	public String getClassifier() {
		return classifier;
	}

	public boolean matches(Artifact artifact) {
		if (!name.equals(artifact.getName()))
			return false;
		if (extension == null ? artifact.getExtension() != null : !extension.equalsIgnoreCase(artifact.getExtension()))
			return false;
		if (classifier == null ? artifact.getClassifier() != null : !classifier.equalsIgnoreCase(artifact.getClassifier()))
			return false;
		if (!version.matches(artifact.getVersion()))
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((classifier == null) ? 0 : classifier.hashCode());
		result = prime * result + ((extension == null) ? 0 : extension.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((version == null) ? 0 : version.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UnresolvedArtifact other = (UnresolvedArtifact) obj;
		if (classifier == null) {
			if (other.classifier != null)
				return false;
		} else if (!classifier.equals(other.classifier))
			return false;
		if (extension == null) {
			if (other.extension != null)
				return false;
		} else if (!extension.equals(other.extension))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (version == null) {
			if (other.version != null)
				return false;
		} else if (!version.equals(other.version))
			return false;
		return true;
	}

	@Override
	public int compareTo(UnresolvedArtifact a) {
		if (a == null) {
			return 1;
		}
		int ret = name.compareTo(a.name);
		if (ret != 0) {
			return ret;
		}
		ret = version.compareTo(a.version);
		if (ret != 0) {
			return ret;
		}
		ret = compare(classifier, a.classifier);
		if (ret != 0) {
			return ret;
		}
		ret = compare(extension, a.extension);
		if (ret != 0) {
			return ret;
		}
		return 0;
	}

	private int compare(String a, String b) {
		if (a == null) {
			if (b != null) {
				return -1;
			}
			return 0;
		}
		if (b == null) {
			return 1;
		}
		int alen = a.length();
		int blen = b.length();

		if (alen != blen) {
			return alen - blen;
		}
		return a.compareToIgnoreCase(b);
	}

	@Override
	public String toString() {
		final StringBuilder builder = new StringBuilder();
		builder.append(name).append('-');
		builder.append(version);
		if (classifier != null) {
			builder.append('-').append(classifier);
		}
		if (extension != null) {
			builder.append('.').append(extension);
		}
		return builder.toString();
	}
}
